package cz.muni.fi.pa165.pa165_spring.service;

import cz.muni.fi.pa165.pa165_spring.convert.PlayerConvert;
import cz.muni.fi.pa165.pa165_spring.dao.PlayerDAO;
import cz.muni.fi.pa165.pa165_spring.dto.PlayerDTO;
import cz.muni.fi.pa165.pa165_spring.model.Player;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class PlayerService {

    @Autowired
    PlayerDAO playerDao;
    @Autowired
    PlayerConvert convert;
    
    public long create(PlayerDTO dto) {
        
        Player player = convert.fromDTOToEntity(dto);
        
        playerDao.create(player);
        
        return player.getId();
    }


    public List<PlayerDTO> findAll() {
        
        Collection<Player> allPlayers = playerDao.findAll();
        List<PlayerDTO> allDtoPlayers = new ArrayList<PlayerDTO>();
        
        for(Player player : allPlayers)
        {
            PlayerDTO dtoPlayer = convert.fromEntityToDTO(player);
            
            allDtoPlayers.add(dtoPlayer);
        }

        
        return allDtoPlayers;
    }
    
}
